<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<div role="main" class="main">
	<div class="center">
		<c:choose>
			<c:when test="${ exception != null}"> 
			<div class="row margintop200">

					<div class="span6 offset3">
						<div class="alert alert-danger">
							<strong>Erreur!</strong> ${exception }
						</div>
					</div>

				</div>
				
			</c:when>
			<c:otherwise>
		<div class="row aligncenter">
			<div class="span11">
				<h2 class="left">Liste des modes de livraison</h2>
				<div class="conteneur-tableau">
					<table class="table table-bordered table-striped bg-bleu" id="modeDeLivraison-table">
						<thead>
							<tr class="header">
								<th>Mode de Livraison</th>
								<th>Coût client classique (€)</th>
								<th>Coût super client (€)</th>
								<th class="td-icon"></th>
							</tr>
						</thead>
						<tbody>
							<c:forEach items="${modes}" var="mode">
								<tr id="${mode.id}">
									<td><c:out value="${mode.libelle}" /></td>
									<td><input id="coutClient-${mode.id}" class="input-mini" type="text" value="<fmt:formatNumber value="${mode.coutClient}" type="NUMBER" minFractionDigits="2"/>" /> </td>
									<td><input id="coutSuperClient-${mode.id}" class="input-mini" type="text" value="<fmt:formatNumber value="${mode.coutSuperClient}" type="NUMBER" minFractionDigits="2"/>" /> </td>
									<td><a class="update"><span class="label label-info"> <i class="icon-film icon-white"></i></span></a></td>
								</tr>
							</c:forEach>
						</tbody>
					</table>
				</div>
				<div></div>
			</div>
		</div>
		<div class="row margintop20 hide" id="divSuccess">
						<div class="span6 offset3">
							<div class="alert alert-success">
							<strong>Succès!</strong>
							Tarifs Bien mis à jour !
							</div>
						</div>
					</div>
		<div class="row margintop20">
			<div class="span4 offset4">
				<a class="btn btn-primary" id="create-admin" href="<%= request.getContextPath() %>/relais.do"> <i
					class="icon-search icon-white"></i> Accéder aux relais
				</a>
			</div>
		</div>

	<script language="javascript">
$(document).ready(function() {
	var prixRegex = /^[0-9]+(,[0-9]{2}|\.[0-9]{2})?$/;
	$('table#modeDeLivraison-table td a.update').click(function()
	{
		$("#divSuccess").addClass("hide");
		var id = $(this).parent().parent().attr('id');
        var idCoutClient = "#coutClient-" + id;
        var idCoutSuperClient = "#coutSuperClient-" + id;
	if(!prixRegex.test($(idCoutClient).val()) || !prixRegex.test($(idCoutSuperClient).val())){
		alert('Veuillez saisir des prix valides');
		return false;
	}
	if (confirm("Etes-vous certain de vouloir mettre à jour ces tarifs?"))
				        {
							var id = $(this).parent().parent().attr('id');
					        var idCoutClient = "#coutClient-" + id;
					        var idCoutSuperClient = "#coutSuperClient-" + id;
				            var coutClient = $(idCoutClient).val().replace(",",".");
				            var coutSuperclient = $(idCoutSuperClient).val().replace(",",".");
			            	var data = "coutClient="+coutClient+"&coutSuperClient="+coutSuperclient+"&id="+id;
				            $.ajax(
				            {
				                   type: "POST",
				                   url: "<%= request.getContextPath() %>/updateModeDeLivraison.do",
				                   data: data,
				                   cache: false,
			 
				                   success: function()
			                   {
				                	$("#divSuccess").removeClass("hide");
				                    
				                   }
				             });
				        }
				    });
});



	</script>
			</c:otherwise>
		</c:choose>
	</div>
</div>